package com.medicine.hbp.fragment.x;

import java.util.List;

import org.kymjs.kjframe.http.HttpParams;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.medicine.hbp.adapter.FragmentAdapter03;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.artGroupJyVo;
import com.medicine.hbp.mob.artGroupListJyVo;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.artGroupVo;
import com.medicine.hbp.mob.findParentAllByTypeVo;
import com.medicine.hbp.sp.InterestSp;
import com.medicine.hbp.sp.UtilsSp;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;
import com.medicine.hbp.xListView.XListView.IXListViewListener;
import com.medicine.hbp.R;

/**
 * 经验界面
 */
public class Fragment03 extends Fragment implements IXListViewListener {
	private View View;
	private XListView xlistview;
	private FragmentAdapter03 adapter;
	private volatile boolean refresh = true;
	private volatile int page = 1;// 当前页（从0开始）
	private int pageSize = 10;
	private List<artGroupJyVo> artGroupVos;
	private ImageView NoNetWork;
	@Override
	public android.view.View onCreateView(LayoutInflater inflater,
										  ViewGroup container, Bundle savedInstanceState) {
		View = inflater.inflate(R.layout.fragment_03, container, false);
		xlistview = (XListView) View.findViewById(R.id.xListView);
		NoNetWork = (ImageView) View.findViewById(R.id.image_no_wifi);
		initView();
		return View;
	}

	private void initView() {
		adapter=new FragmentAdapter03(getActivity());
		xlistview.setAdapter(adapter);
		xlistview.setXListViewListener(this);
		if(NetWorkUtils.getAPNType(getActivity())==0){
			xlistview.setVisibility(View.GONE);
			NoNetWork.setVisibility(View.VISIBLE);
		}else{
			xlistview.setVisibility(View.VISIBLE);
			NoNetWork.setVisibility(View.GONE);
			httpLaberGroupList();
		}
		NoNetWork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(getActivity())==0){
					xlistview.setVisibility(View.GONE);
					NoNetWork.setVisibility(View.VISIBLE);
					ToastUtil.showShort(getActivity(), "断网啦！");
				}else{
					xlistview.setVisibility(View.VISIBLE);
					NoNetWork.setVisibility(View.GONE);
					httpLaberGroupList();
				}
			}
		});
	}
	/**
	 * 首页标签组获取
	 */
	private void httpLaberGroupList() {
		HttpParams params = new HttpParams();
		params.put("current", page);// 当前页数(不传默认为1)
		params.put("rowCount", pageSize);// 每页显示数(不传默认为10)
		AppHttps.HttpPost(getActivity(),HttpInterface.ART_EXPERIENCEL_IST, params,
				new AppHttpCallBack<artGroupListJyVo>(artGroupListJyVo.class, getActivity()) {
					@Override
					public void onSuccess(artGroupListJyVo result) {
						boolean hasMore = true;
						artGroupVos = result.artInfo;
						if (refresh) {
							adapter.changeData(artGroupVos);
						} else {
							if(UtilsSp.getIsYm(getActivity())){
								artGroupJyVo vo=new artGroupJyVo();
								vo.isbanner=true;
								artGroupVos.add(vo);
							}
							adapter.addItems(artGroupVos );
							adapter.notifyDataSetChanged();
						}
						if (result.artInfo.size() < pageSize) {
							hasMore = false;
						}
						xlistview.setPullLoadEnable(hasMore);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(getActivity(), errorMsg);
					}
					@Override
					public void onFinish() {
						super.onFinish();
						xlistview.setRefreshTime(CommUtil.formatDate(System.currentTimeMillis()));
					}
				});

	}

	@Override
	public void onRefresh() {
		page=1;
		refresh = true;
		xlistview.setPullLoadEnable(false);
		httpLaberGroupList();
	}

	@Override
	public void onLoadMore() {
		page++;
		refresh = false;
		httpLaberGroupList();
	}
}